/**
 * 
 */
package com.ioffice.export.excel.template;

import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
/**
 * @author <a href="mailto:goodluck.sunlight@gmail.com">chenboning</a>
 *
 * 
 * @descption <b>简单的Excel生成</b>
 *
 * @version 1.0
 *
 *
 * @time  2013-5-22  下午3:59:12
 */
public class ExportSimpleExcelTemplate {

	public void writeExcel(OutputStream out,String[] colNames,String[][] records,String sheetName) throws IOException{
		int startIndex = 0;
		Workbook wb = new HSSFWorkbook();
		startIndex = writeoneSheet(wb,records,colNames,startIndex,65535,sheetName);
		wb.write(out);
	}
	
	public void writeExcel(OutputStream out,String[] colNames,String sheetName) throws IOException{
		Workbook wb = new HSSFWorkbook();
		if(colNames.length>0){
			writeoneSheet(wb,colNames,sheetName);
		}
		wb.write(out);
	}
	
	
	private int writeoneSheet(Workbook wb,String[][] records,String[] colNames,int startIndex,int oneSheetCount,String sheetName){
		Sheet dataSheet = wb.createSheet(sheetName);
		Row row = null;
		Cell cell = null;
		int rowIndex = 0;
		int colCount = colNames.length;
		int cellIndex = 0;
		//写列名
		CellStyle colNameCellStyle = wb.createCellStyle();
		colNameCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
		row = dataSheet.createRow(rowIndex);
		for(int i=0;i<colCount;i++){
			cellIndex  = i;
			cell = row.createCell(cellIndex);
			cell.setCellStyle(colNameCellStyle);
			cell.setCellValue(colNames[i]);
		}
		rowIndex++;
		//写数据
		String[] record;
		int endIndex = startIndex+oneSheetCount;
		CellStyle dataCellStyle = wb.createCellStyle();
		dataCellStyle.setWrapText(false);
		dataCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
		for (; startIndex < records.length && startIndex < endIndex; startIndex++) {
			record = records[startIndex];
			row = dataSheet.createRow(rowIndex);
			for (int j = 0; j < colCount; j++) {
				cellIndex  = j;
				cell = row.createCell(cellIndex);
				cell.setCellStyle(dataCellStyle);
				cell.setCellValue(record[j]);
			}
			rowIndex++;
		}
		
		return startIndex;
	}
	
	
	private void writeoneSheet(Workbook wb,String[] colNames,String sheetName){
		if(sheetName!=null&&sheetName.length()<0){
			sheetName = "Sheet1";
		}
		Sheet dataSheet = wb.createSheet(sheetName);
		Row row = null;
		Cell cell = null;
		int rowIndex = 0;
		int colCount = colNames.length;
		int cellIndex = 0;
		//写列名
		CellStyle colNameCellStyle = wb.createCellStyle();
		colNameCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		colNameCellStyle.setFillForegroundColor((short)10);
		colNameCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
		row = dataSheet.createRow(rowIndex);
		for(int i=0;i<colCount;i++){
			cellIndex  = i;
			cell = row.createCell(cellIndex);
			cell.setCellStyle(colNameCellStyle);
			cell.setCellValue(colNames[i]);
		}
	}
}
